﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using YourFileWatcher.Models;

namespace YourFileWatcher
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            //UserInfo u = new UserInfo();
            //u.Name = "张哥管理员";
            //u.LoginId = "admin";
            //u.Password = "123";
            //u.UserType = UserType.Admin;
            //u.SerilizeFile("Content");
        }


        /// <summary>
        /// 当前登录的用户
        /// </summary>
        public static UserInfo LoginUser { get; set; }


        private MainDataInfo _MainData = new MainDataInfo();
        /// <summary>
        /// 主要交互数据
        /// </summary>
        public MainDataInfo MainData
        {
            get { return _MainData; }
            set
            {
                _MainData = value;
                RaisePropertyChanged("MainData");
            }
        }



        #region 接口 INotifyPropertyChanged


        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if (null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }


        #endregion


        /// <summary>
        /// 成功登陆，初始化用户信息
        /// </summary>
        private void InitializeUserHabbitInternal()
        {
            if (LoginUser.UserType == UserType.Admin)
            {
                //管理员，加载管理模块
                this.tabItemLogListing.Visibility = Visibility.Visible;
                this.tabItemAdmin.Visibility = Visibility.Visible;
                MainData.LoadUsers();
                MainData.LoadLogFiles();
            }
        }



        #region 事件处理

        /// <summary>
        /// 主窗体加载完毕后，尝试登陆用户
        /// </summary>
        private void Window_ContentRendered(object sender, EventArgs e)
        {
            var loginWindow = new LoginWindow();
            loginWindow.Owner = this;
            loginWindow.MainWindow = this;
            this.Hide();
            bool? re = loginWindow.ShowDialog();

            if (re == true)
            {
                // 成功登陆，初始化用户信息
                InitializeUserHabbitInternal();
                this.Show();
            }
            else
            {
                // 取消登录，程序退出
                this.Close();
            }
        }


        /// <summary>
        /// 添加监视目录
        /// </summary>
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            string path = Ser.LocalFile.DialogSelectDirectoryGetPath();

            if (!String.IsNullOrEmpty(path))
            {
                MainData.AddWatcherItem(path);
            }
        }

        /// <summary>
        /// 打开文件位置
        /// </summary>
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (null != MainData.SelectedLog)
            {
                Ser.LocalFile.ExplorerPath(MainData.SelectedLog.FullName);
            }
        }

        /// <summary>
        /// 打开文件位置
        /// </summary>
        private void MenuItemLogFile_Click(object sender, RoutedEventArgs e)
        {
            if (null != MainData.SelectedLogFile)
            {
                Ser.LocalFile.ExplorerPath(MainData.SelectedLogFile.FullName);
            }
        }

        /// <summary>
        /// 删除日志文件
        /// </summary>
        private void MenuItemDeleteLogFile_Click(object sender, RoutedEventArgs e)
        {
            if (null!=MainData.SelectedLogFile)
            {
                MainData.DeleteLogFile();
            }
        }

        /// <summary>
        /// 清空所有Log
        /// </summary>
        private void btnClearLogs_Click(object sender, RoutedEventArgs e)
        {
            MainData.Logs.Clear();
        }

        /// <summary>
        /// 将日志信息保存到日志目录下，支持追踪列表管理
        /// </summary>
        private void btnSaveFile_Click(object sender, RoutedEventArgs e)
        {
            if (MainData.Logs.Count == 0)
            {
                return;
            }

            MainData.SaveLogFile();
        }

        /// <summary>
        /// 将日志信息另存为文件，位置由用户自由指定，不支持追踪列表管理
        /// </summary>
        private void btnSaveAsLogFile_Click(object sender, RoutedEventArgs e)
        {
            if (MainData.Logs.Count == 0)
            {
                return;
            }
            MainData.SaveAsLogFile();
        }


        /// <summary>
        /// 监视全部
        /// </summary>
        private void btnWatchAll_Click(object sender, RoutedEventArgs e)
        {
            foreach (var iWatcher in MainData.Watchers)
            {
                iWatcher.IsWork = true;
            }
        }

        /// <summary>
        /// 全部不监视
        /// </summary>
        private void btnWatchNon_Click(object sender, RoutedEventArgs e)
        {
            foreach (var iWatcher in MainData.Watchers)
            {
                iWatcher.IsWork = false;
            }
        }


        /// <summary>
        /// 创建角色
        /// </summary>
        private void btnCreateUser_Click(object sender, RoutedEventArgs e)
        {
            UserInfo newUser = MainData.CreateUser;

            if (!String.IsNullOrWhiteSpace(newUser.LoginId)
                && !String.IsNullOrWhiteSpace(newUser.Name)
                && !String.IsNullOrWhiteSpace(newUser.Password)
                && MainData.Users.All(dd => dd.LoginId != newUser.LoginId))
            {
                //尝试创建用户
                if (MainData.CreateNewUser())
                {
                    MainData.Users.Add(MainData.CreateUser);
                }
                MainData.CreateUser = new UserInfo();
            }
            else
            {
                //无法新建用户，信息填写不完整或者已经存在LoginId

            }

        }

        /// <summary>
        /// 删除用户
        /// </summary>
        private void btnDeleteUser_Click(object sender, RoutedEventArgs e)
        {
            if (MainData.DeleteUser())
            {
                //删除成功
                MainData.Users.Remove(MainData.SelectedUser);
            }
        }

        #endregion



    }
}
